﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinformText4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        #region 保存密码的代码
        [Serializable]

        public class User
        {

            private string loginID;
            public string LoginID
            {
                get { return loginID; }
                set { loginID = value; }
            }

            private string pwd;
            public string Pwd
            {
                get { return pwd; }
                set { pwd = value; }
            }
        }
        //显示用户所对应匹配的信息
        private void DisplayUserInfo()
        {

            string key = comboBox1.Text.Trim();
            //查找用户Id
            if (users.ContainsKey(key) == false)
            {
                textBox1.Text = "";
                return;
            }
            //查找到赋值
            User user = users[key];
            textBox1.Text = user.Pwd;
            // 如有有密码 选中复选框
            checkBox1.Checked = textBox1.Text.Trim().Length > 0 ? true : false;
        }
        #endregion


        Dictionary<string, User> users = new Dictionary<string, User>();
        private void Form1_Load(object sender, EventArgs e)
        {
            #region 保存密码
            FileStream fs = new FileStream("data.bin", FileMode.OpenOrCreate);
            if (fs.Length > 0)
            {
                BinaryFormatter bf = new BinaryFormatter();
                //读出存在Data.bin 里的用户信息
                users = bf.Deserialize(fs) as Dictionary<string, User>;
                //循环添加到Combox1
                foreach (User user in users.Values)
                {
                    comboBox1.Items.Add(user.LoginID);
                }

                //combox1 用户名默认选中第一个
                if (comboBox1.Items.Count > 0)
                    comboBox1.SelectedIndex = comboBox1.Items.Count - 1;
            }
            fs.Close();

            #endregion
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text == "" || textBox1.Text == "")
            {
                MessageBox.Show("提示：请输入用户名和密码！", "警告");
            }


            var sql = "select * from Users where Username='" + comboBox1.Text.Trim() + "' and Password='" + textBox1.Text.Trim() + "'";
            SqlConnection conn = new SqlConnection("server=LAPTOP-NT2O3C2R\\SQLEXPRESS;uid=xiaoyou;pwd=123456;database=Blogs");
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader sdr = cmd.ExecuteReader();
            sdr.Read();
            if (sdr.HasRows)
            {
                MessageBox.Show("登录成功!", "提示");
                MainForm mainForm = new MainForm();
                mainForm.Show();
            }

            else
            {
                MessageBox.Show("提示：学生用户名或密码错误!", "警告");
                conn.Close();
            }

            #region 保存密码的代码
            User user = new User();
            // 登录时 如果没有Data.bin文件就创建、有就打开
            FileStream fs = new FileStream("data.bin", FileMode.OpenOrCreate);
            BinaryFormatter bf = new BinaryFormatter();
            // 保存在实体类属性中
            user.LoginID = comboBox1.Text.Trim();
            //保存密码选中状态
            if (checkBox1.Checked)
                user.Pwd = textBox1.Text.Trim();
            else
                user.Pwd = "";
            //选在集合中是否存在用户名 
            if (users.ContainsKey(user.LoginID))
            {
                //如果有清掉
                users.Remove(user.LoginID);
            }
            //添加用户信息到集合
            users.Add(user.LoginID, user);
            //写入文件
            bf.Serialize(fs, users);
            //关闭
            fs.Close();
            MessageBox.Show("保存密码成功！请关闭窗体看效果");

            #endregion
        }

        private void button2_Click(object sender, EventArgs e)
        {
            RegisterForm register = new RegisterForm();

            register.Show();

        }
        private void combox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DisplayUserInfo();
        }
        private void combox1_TextChanged(object sender, EventArgs e)
        {
            DisplayUserInfo();

        }
        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            #region 显示密码
            if (checkBox2.Checked)
            {
                //复选框被勾选，明文显示
                textBox1.PasswordChar = new char();

            }
            else
            {
                //复选框被取消勾选，密文显示
                textBox1.PasswordChar = '*';
            }
            #endregion
        }
    }
}
